#include "matrix_float.h"

//private
void matrix_float::create_matrix(int r, int c){
	if (r == 0 || c == 0){
		r_size = 0;
		c_size = 0;
		mptr = nullptr;
	}
	else if (r > 0 && c > 0){
		mptr = new float*[r];
		for (int i = 0; i < r; i++){
			mptr[i] = new float[c];
		}
		r_size = r;
		c_size = c;
	}
	else{
		cout << "Error - Invalid size. Size must be above 0!" << endl;
		r_size = 0;
		c_size = 0;
		*mptr = nullptr;
	}
}

void matrix_float::delete_matrix(){
	//clear columns
	for (int i = 0; i < r_size; i++){
		delete mptr[i];
	}
	delete[] mptr;
}


//public
void matrix_float::set(float value, int r, int c){
	if (r >= 0 && r < r_size && c >= 0 && c < c_size){
		mptr[r][c] = value;
	}
	else{
		cout << "Error - Invalid index: (" << r << "," << c << ")! Matrix size is (" << r_size << "," << c_size << ")." << endl;
	}
}

void matrix_float::read_from_keyboard(){
	float value;
	for (int i = 0; i < r_size; i++)
	{
		for (int j = 0; j < c_size; j++)
		{
			cout << "Enter element # (" << i << "," << j << "): ";
			cin >> value;
			set(value, i, j);
		}
	}
}

void matrix_float::print(){
	for (int i = 0; i < r_size; i++)
	{
		cout << "|";
		for (int j = 0; j < c_size; j++)
		{
			cout << mptr[i][j];
			if (j < c_size - 1){
				cout << ",";
			}
		}
		cout << "|" << endl;
	}
}


//constructors
matrix_float::matrix_float()
{
	create_matrix(0, 0);
}

matrix_float::matrix_float(int r, int c)
{
	create_matrix(r, c);
}

matrix_float::matrix_float(matrix_float& m){
	create_matrix(m.r_size, m.c_size);
	for (int i = 0; i < m.r_size; i++)
	{
		for (int j = 0; j < m.c_size; j++)
		{
			set(m.mptr[i][j], i, j);
		}
	}
}


//destructor
matrix_float::~matrix_float()
{
	delete_matrix();
}


//operators
matrix_float & matrix_float::operator=(matrix_float& m){
	create_matrix(m.r_size, m.c_size);
	for (int i = 0; i < m.r_size; i++)
	{
		for (int j = 0; j < m.c_size; j++)
		{
			set(m.mptr[i][j], i, j);
		}
	}
	return *this;
}